const Menu = require('./Menu')
const Permission = require('./Permission')
const Role = require('./Role')
const User = require('./User')
const Collect = require('./Collect')
const CollectType = require('./CollectType')
const Password = require('./Password')
const Memery = require('./Memery')
const Account = require('./Account')
const AccountTag = require('./AccountTag')
const Task = require('./Task')
const Project = require('./Project')
const ProjectDocument = require('./ProjectDocument')
const ProjectImage = require('./ProjectImage')
const ProjectTask = require('./ProjectTask')

// 菜单与菜单自关联
Menu.hasMany(Menu, { as: 'children', foreignKey: 'pid', through: null }) // 自身关联

// Permission.belongsTo(Tag, { as: 'tid', foreignKey: 'tag', through: null }) // Permission -> Tag: 一对多

// role与permission 多对多关联
Role.belongsToMany(Permission, { through: 'role_perm', as: 'rp' })
Permission.belongsToMany(Role, { through: 'role_perm', as: 'rp' })

// role与menu
Role.belongsToMany(Menu, { through: 'role_menu' })
Menu.belongsToMany(Role, { through: 'role_menu' })

// user和role 多对多关联
User.belongsToMany(Role, { through: 'user_role', as: 'ur' })
Role.belongsToMany(User, { through: 'user_role', as: 'ur' })

// collect和collect_type和user 
CollectType.hasMany(Collect, { foreignKey: 'tid', sourceKey: 'id' }) //// 一对多
Collect.belongsTo(CollectType, { foreignKey: 'tid', sourceKey: 'id' })
Collect.belongsTo(User, { foreignKey: 'uid', sourceKey: 'id' })
CollectType.belongsTo(User, { foreignKey: 'uid', sourceKey: 'id' })

// 一对多
Password.belongsTo(User, { foreignKey: 'uid', sourceKey: 'id' })

// 一对多
Memery.belongsTo(User, { foreignKey: 'uid', sourceKey: 'id' })

// 一对多
Account.belongsTo(User, { foreignKey: 'uid', sourceKey: 'id' })

// 一对多
AccountTag.belongsTo(User, { foreignKey: 'uid', sourceKey: 'id' })

// 一对多
Task.belongsTo(User, { foreignKey: 'uid', sourceKey: 'id' })

// 一对多
Project.belongsTo(User, { foreignKey: 'uid', sourceKey: 'id' })
ProjectImage.belongsTo(Project, { foreignKey: 'pid', sourceKey: 'id' })
ProjectTask.belongsTo(Project, { foreignKey: 'pid', sourceKey: 'id' })
ProjectDocument.belongsTo(Project, { foreignKey: 'pid', sourceKey: 'id' })


module.exports = {
  Menu,
  Permission,
  Role,
  User,
  Collect,
  CollectType,
  Password,
  Memery,
  Account,
  AccountTag,
  Task,
  Project,
  ProjectDocument,
  ProjectImage,
  ProjectTask
}
